import com.gule.gl.leetcode.editor.cn.definition.TreeNode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;

/**
 * @ClassName: leetcode-study Test2
 * @Description: PACKAGE_NAME
 * @Author: 逸伦
 * @Date: 2023/8/5 21:56
 * @Version: 1.0
 */
public class Test2 {

	public static void main(String[] args) throws InterruptedException {
		CCC.threeThreadPrint();
		Thread.sleep( 10 * 1000);
	}

	private List<List<Integer>> levelQuery(TreeNode root){
		if(Objects.isNull(root)){
			return new ArrayList<>();
		}
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);

		List<List<Integer>> finalRes = new ArrayList<>();
		while (!queue.isEmpty()){
			List<Integer> level = new ArrayList<>();
			int size = queue.size();
			for(int i=0;i<size;i++){
				TreeNode poll = queue.poll();
				level.add(poll.val);
				if(Objects.nonNull(poll.left)){
					queue.offer(poll.left);
				}
				if(Objects.nonNull(poll.right)){
					queue.offer(poll.right);
				}
			}
			finalRes.add(level);
		}
		return finalRes;
	}

	public static class CCC implements Serializable {
		public static Object lock = new Object();
		public static Integer flag = 0;
		public static Integer count = 0;

		public static void threeThreadPrint() {

			new Thread(() -> {
				for(;;) {
					synchronized (lock) {
						if (flag == 0) {
							System.out.println("thread name = " + Thread.currentThread().getName() + "current count is = " + count++);
							flag = 1;
						}else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								throw new RuntimeException(e);
							}
						}
						if (count > 100) {
							System.out.println("thread name = " + Thread.currentThread().getName() + " current count is max, thread out");
							break;
						}
						lock.notify();
					}
				}
			}).start();
			new Thread(() -> {
				for(;;) {
					synchronized (lock) {
						if (flag == 1) {
							flag = 2 ;
							System.out.println("thread name = " + Thread.currentThread().getName() + "current count is = " + count++);
						}else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								throw new RuntimeException(e);
							}
						}
						if (count > 100) {
							System.out.println("thread name = " + Thread.currentThread().getName() + " current count is max, thread out");
							break;
						}

						lock.notify();
					}
				}
			}).start();
			new Thread(() -> {
				for(;;) {
					synchronized (lock) {
						if (flag == 2) {
							flag = 0;
							System.out.println("thread name = " + Thread.currentThread().getName() + "current count is = " + count++);
						}else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								throw new RuntimeException(e);
							}
						}
						if (count > 100) {
							System.out.println("thread name = " + Thread.currentThread().getName() + " current count is max, thread out");
							break;
						}

						lock.notify();
					}
				}
			}).start();
		}
	}
}
